package com.demo.blog.service;

import com.demo.blog.mapper.BlogInfoMapper;
import com.demo.blog.mapper.UserInfoMapper;
import com.demo.blog.model.Blog;
import com.demo.blog.model.User;
import com.demo.blog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-07-24
 * Time: 14:24
 */
@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public User queryByName(String userName) {
        return userInfoMapper.queryByName(userName);
    }

    public User queryById(Integer userId) {
        return userInfoMapper.queryById(userId);
    }
    public User getAuthorInfo(Integer blogId) {
        //1. 根据blogId 获取userId
        //2. 根据userid 获取userInfo
        Blog blogInfo = blogInfoMapper.queryById(blogId);
        if (blogInfo==null || blogInfo.getUserId()<0){
            return null;
        }
        return userInfoMapper.queryById(blogInfo.getUserId());
    }

    public boolean insertUser(String userName, String password){
        //加盐
        String encry_password = SecurityUtils.encry(password);

        return userInfoMapper.insertUser(userName, encry_password);
    }
}
